package com.good.child.ui.adapter.ai;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.good.child.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AiPictureAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<String> dataList;
    public AiPictureAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<String> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_picture, parent, false);
            AiPictureAdapter.AiPictureViewHolder myViewHolder = new AiPictureAdapter.AiPictureViewHolder(inflater);
            return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }
        String imageUrl = dataList.get(position);

        AiPictureViewHolder viewHolder= (AiPictureViewHolder) holder;
        Uri uri=Uri.parse(imageUrl);
        Glide.with(context).load(uri).into(viewHolder.imPicture);
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 1 : dataList.size();
    }

    class AiPictureViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.im_picture)
        ImageView imPicture;

        public AiPictureViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
